/**
 * 弹窗方向
 *
 * - 含义：窗口位于按钮的什么方向；
 * - 以窗口为参照物，方向都是反着的；
 */
export type Direction = 'top' | 'bottom' | 'left' | 'right' | string;

/**
 * 对齐方式
 *
 * 吸附窗口与吸附组件之间的对齐方式
 */
export type Align = 'start' | 'end' | 'center' | string;

/**
 * 定义矩形区域的接口，描述 DOMRect 的结构
 */
class Rect {
    top: number = 0;
    left: number = 0;

    bottom: number = 0;
    right: number = 0;

    height: number = 0;
    width: number = 0;

    x: number = 0;
    y: number = 0;

    constructor(dom: HTMLElement | undefined = undefined) {
        if (dom !== undefined) {
            this.ofDOM(dom);
        }
    }

    /**
     * 从 DOMRect 对象复制属性值
     *
     * @param rect - DOMRect 对象，包含元素的几何信息
     * @returns 当前 Rect 实例（支持链式调用）
     */
    ofDOMRect(rect: DOMRect): this {
        const {bottom, height, left, right, top, width, x, y} = rect;
        Object.assign(this, {bottom, height, left, right, top, width, x, y});
        return this;
    }

    /**
     * 从 DOM 元素获取几何信息并复制到当前实例
     *
     * @param dom - 目标 HTMLElement 元素
     * @returns 当前 Rect 实例（支持链式调用）
     */
    ofDOM(dom: HTMLElement): this {
        return this.ofDOMRect(dom.getBoundingClientRect());
    }

    /**
     * 放大 n 个值
     *
     * @returns 当前 Rect 实例（支持链式调用）
     */
    zoom(num: number): this {
        this.top = this.top - num;
        this.left = this.left - num;
        this.bottom = this.bottom + num;
        this.right = this.right + num;
        this.width = this.width + num * 2;
        this.height = this.height + num * 2;
        // this.x = this.x - num;
        // this.y = this.y - num;
        return this;
    }

    /**
     * 设置坐标
     *
     * @param location -
     */
    setLocation(location: [number, number]) {
        this.top = location[1]
        this.left = location[0];
        this.y = location[1]
        this.x = location[0];
        this.bottom = this.top + this.height;
        this.right = this.left + this.width;
    }

    /**
     * 获取坐标
     */
    getLocation(): [number, number] {
        return [this.top, this.left];
    }
}

export default Rect;